<%@ page language="java" pageEncoding="utf-8"%>

<%@ page import="java.util.*,org.jawa.util.*"%>

<jsp:useBean id="webManager" class="org.jawa.util.WebManager" />
<%
	webManager.init(request, response, session, application, out);
	String path = request.getContextPath();
%>
<%
	String host = ParamUtils.getParameter(request, "host");
	int port = ParamUtils.getIntParameter(request, "port", 0);
	String username = ParamUtils.getParameter(request, "server_username");
	String password = ParamUtils.getParameter(request, "server_password");
	boolean ssl = ParamUtils.getBooleanParameter(request, "ssl");
	boolean save = request.getParameter("save") != null;
	boolean test = request.getParameter("test") != null;
	boolean debug = ParamUtils.getBooleanParameter(request, "debug");

	if (test) {
		response.sendRedirect(path+"/admin/system-emailtest.jsp");
		return;
	}

	EmailService service = EmailService.getInstance();
	Map<String, String> errors = new HashMap<String, String>();
	if (save) {
		if (host != null) {
			service.setHost(host);
		} else {
			errors.put("host", "邮件主机不可以为空!");
		}
		if (port > 0) {
			service.setPort(port);
		} else {
			service.setPort(25);
		}
		service.setUsername(username);
		String existingHashPassword = "";
		if (service.getPassword() != null) {
			existingHashPassword = StringUtils.hash(service.getPassword());
		}

		if (!existingHashPassword.equals(password)) {
			String newHashPassword = "";
			if (password != null) {
				newHashPassword = StringUtils.hash(password);
			}
			if (!existingHashPassword.equals(newHashPassword)) {
				service.setPassword(password);
			}
		}

		service.setDebugEnabled(debug);
		service.setSSLEnabled(ssl);

		if (errors.size() == 0) {
			webManager.logEvent("邮件SMTP设置已更新", "host = "+host+"\nport = "+port+"\nusername = "+username);
			JawaGlobals.setProperty("mail.configured", "true");
			response.sendRedirect(path+"/admin/system-email.jsp?success=true");
		}
	}

	host = service.getHost();
	port = service.getPort();
	username = service.getUsername();
	password = service.getPassword();
	ssl = service.isSSLEnabled();
	debug = service.isDebugEnabled();
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>邮件设置</title>
		<meta name="pageID" content="1107" />
	</head>
	<body>
		<div class="row-fluid">
			<ul class="breadcrumb">
				<li>
					您现在的位置:
				</li>
				<li>
					<a href="<%=path%>/admin/index.jsp">服务器</a>
				</li>
				<li>
					<span class="divider">/</span>
				</li>
				<li class="active">
					邮件设置
				</li>
			</ul>

			<div class="page-header">
				<h3>
					邮件设置
				</h3>
			</div>


			<%
				if ("true".equals(request.getParameter("success"))) {
			%>
				<div class="alert alert-success fade in">
					<a class="close" data-dismiss="alert" href="#">×</a>
					<i class="icon-ok-circle icon-white"></i><strong>SMTP设置更新成功!</strong>
				</div>
			<%
				}
			%>

			<%
				if (errors.size() > 0) {
			%>
				<div class="alert alert-error fade in">
					<a class="close" data-dismiss="alert" href="#">×</a>
					<i class="icon-remove-circle icon-white "></i><strong>错误－SMTP设置更新失败!</strong>
				</div>
			<%
				}
			%>


			<form action="<%=path%>/admin/system-email.jsp" name="emailform" id="emailform"
				method="post" class="form-horizontal well">
				<fieldset>
          			<legend>SMTP设置</legend>
					<div class="control-group <%=errors.containsKey("host") ? "error" : ""%>" >
						<label for="focusedInput" class="control-label">
							邮件服务器:
						</label>
						<div class="controls">
							<input type="text" name="host" value="<%=(host != null) ? host : ""%>"
								class="input-xlarge " maxlength="150" />
								<%
							 		if (errors.containsKey("host")) {
								%>
								<span class="help-inline"> 
							  		<%=errors.get("host")%> 
							 	</span>
							  	<%
							 	    }
							 	%> 
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							服务器端口:
						</label>
						<div class="controls">
							<input type="text" name="port" value="<%=(port > 0) ? String.valueOf(port) : ""%>"
								class="input-xlarge " maxlength="15" />
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							服务器用户名:
						</label>
						<div class="controls">
							<input type="text" name="server_username" value="<%=(username != null) ? username : ""%>"
								class="input-xlarge " maxlength="15" />
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							服务器密码:
						</label>
						<div class="controls">
							<input type="password" name="server_password" value="<%=(password != null) ? StringUtils.hash(password) : ""%>"
								class="input-xlarge focused" maxlength="15" />
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							使用SSL:
						</label>
						<div class="controls">
						    <label class="checkbox inline">
								<input type="checkbox" name="ssl" <%=(ssl) ? " checked" : ""%> class="input-xlarge " />
            			     </label>
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							邮件调试:
						</label>
						<div class="controls">
							<label class="radio">
				                <input type="radio" name="debug" value="true"  <%=(debug ? " checked=\"checked\" " : "")%>  id="rb01">
				                                           打开
			                </label>
							<label class="radio">
				                <input type="radio" name="debug" value="false" <%=(!debug ? " checked=\"checked\" " : "")%> id="rb02">
				                                          关闭
			                </label>
						</div>
					</div>

					<div class="form-actions">
						<input type="submit" name="save" value="保存设置"
							class="btn btn-primary">
						<input type="submit" name="test" value="发送测试邮件" class="btn">
					</div>
				</fieldset>	
			</form>

		</div>
		<script src="<%= path %>/scripts/bootstrap.js"></script>
		<script src="<%= path %>/scripts/jquery.validate.js"></script>
		<script>
			$().ready(function() {
				$("#emailform").validate({
					errorClass: "validate-error",
					validClass: "validate-success",
					errorElement:'span',
					errorPlacement: function(error, element) {
						element.parent().parent().addClass('error');
						error.appendTo(element.parent());
					},
					success: function(label) {
						label.html('√');
						label.parent().parent().removeClass("error");
						label.parent().parent().addClass("success");
					},
					rules: {
						host: {
							required:true
						},
						port: {
							digits:true
						}
					},
					messages: {
						host: {
						  required:'请输入邮件服务器!'
						},
						port: {
							digits:'服务器端口应该是一个整数!'
						}						
					}
				});
			});			
		</script>
	</body>
</html>